<template>
  <el-row :gutter="20"
          id="designProductConsStyle">
    <el-form :model="requestParams"
             ref="form"
             :rules="rules"
             :disabled="todos==3?true:false">
      <el-col :span="24">
        <el-col :span="8">
          <el-form-item label="组织名称:"
                        size="medium"
                        :prop="todos==3?'':'jt_org_id'"
                        label-width="82px">
            <el-select v-model="requestParams.jt_org_id"
                       filterable
                       placeholder="请选择"
                       size="medium"
                       class="select-style"
                       :disabled="todos==3?true:false">
              <el-option v-for="item in orgArr"
                         :key="item.jt_org_id"
                         :label="item.org_name"
                         :value="item.jt_org_id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="基地名称:"
                        size="medium"
                        :prop="todos==3?'':'base_name'"
                        label-width="82px">
            <el-input v-model="requestParams.base_name"
                      size="medium"
                      placeholder="请输入基地名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="基地简称:"
                        size="medium"
                        label-width="82px">
            <el-input v-model="requestParams.nick_name"
                      size="medium"
                      placeholder="请输入项目简称"></el-input>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="24">
        <el-col :span="8">
          <el-form-item label="创建日期:"
                        size="medium"
                        :prop="todos==3?'':'add_time'"
                        label-width="82px">
            <el-date-picker v-model="requestParams.add_time"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          &nbsp;
        </el-col>
      </el-col>
      <el-col :span="24">
        <p>面积</p>
      </el-col>
      <el-col :span="24">
        <el-col :span="8">
          <el-form-item label="基地面积(m²)"
                        label-width="110px"
                        :prop="todos==3?'':'base_area'">
            <el-input-number v-model="requestParams.base_area"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入基地面积(m²)"
                             :precision="2"
                             :step="0.1"
                             :min="0"
                             :max="1000000"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="实验室面积(m²)"
                        label-width="110px">
            <el-input-number v-model="requestParams.lab_area"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入实验室面积(m²)"
                             :precision="2"
                             :step="0.1"
                             :min="0"
                             :max="1000000"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="堆场面积(m²)"
                        label-width="110px">
            <el-input-number v-model="requestParams.yard_area"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入堆场面积(m²)"
                             :precision="2"
                             :step="0.1"
                             :min="0"
                             :max="1000000"></el-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="24">
        <el-col :span="8">
          <el-form-item label="车间面积(m²)"
                        label-width="110px">
            <el-input-number v-model="requestParams.workshop_area"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入车间面积(m²)"
                             :precision="2"
                             :step="0.1"
                             :min="0"
                             :max="1000000"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设计产能(m²)"
                        label-width="110px">
            <el-input-number v-model="requestParams.design_capa"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入设计产能(m²)"
                             :precision="2"
                             :step="0.1"
                             :min="0"
                             :max="1000000"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          &nbsp;
        </el-col>
      </el-col>
      <el-col :span="24">
        <p>人数</p>
      </el-col>
      <el-col :span="24">
        <el-col :span="8">
          <el-form-item label="技术人员数量"
                        label-width="110px"
                        :prop="todos==3?'':'skill_peo_num'">
            <el-input-number v-model="requestParams.skill_peo_num"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入技术人员数量"
                             :precision="0"
                             :min="0"
                             :max="10000"
                             :step="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="管理人员数量"
                        label-width="110px"
                        :prop="todos==3?'':'manage_peo_num'">
            <el-input-number v-model="requestParams.manage_peo_num"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入管理人员数量"
                             :precision="0"
                             :min="0"
                             :max="10000"
                             :step="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="生产人员数量"
                        label-width="110px"
                        :prop="todos==3?'':'pro_peo_num'">
            <el-input-number v-model="requestParams.pro_peo_num"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入生产人员数量"
                             :precision="0"
                             :min="0"
                             :max="10000"
                             :step="1"></el-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="24">
        <p>产线数</p>
      </el-col>
      <el-col :span="24">
        <el-col :span="8">
          <el-form-item label="流水线条数"
                        label-width="110px"
                        :prop="todos==3?'':'ass_line'">
            <el-input-number v-model="requestParams.ass_line"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入流水线条数"
                             :precision="0"
                             :min="0"
                             :max="10000"
                             :step="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="固定台模数"
                        label-width="110px">
            <el-input-number v-model="requestParams.fixed_model_num"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入固定台模数"
                             :precision="0"
                             :min="0"
                             :max="10000"
                             :step="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="模台数量"
                        label-width="110px"
                        :prop="todos==3?'':'model_num'">
            <el-input-number v-model="requestParams.model_num"
                             controls-position="right"
                             size="medium"
                             placeholder="请输入模台数量"
                             :precision="0"
                             :min="0"
                             :max="10000"
                             :step="1"></el-input-number>
          </el-form-item>
        </el-col>
      </el-col>
      <el-col :span="24"
              class="bottom-box"
              v-show="todos==3?false:true">
        <el-button size="medium"
                   @click="onCancel">取消</el-button>
        <el-button type="primary"
                   size="medium"
                   @click="onSure('form')">确定</el-button>
      </el-col>
    </el-form>
  </el-row>
</template>

<script>
import moment from 'moment'
import { ProductManageApi, DesignManageApi } from '@/services/WorkCenter'
import { standardCodeApi } from '@/services/StandardCode'
export default {
  props: {
    todos: Number,
    catchBack: Function,
    catchSave: Function,
    detailObj: Object
  },
  data () {
    return {
      orgArr: [],
      requestParams: {
        base_name: '',
        nick_name: '',
        base_area: undefined,
        lab_area: undefined,
        yard_area: undefined,
        design_capa: undefined,
        skill_peo_num: undefined,
        manage_peo_num: undefined,
        pro_peo_num: undefined,
        ass_line: undefined,
        fixed_model_num: undefined,
        model_num: undefined,
        jt_org_id: '',
        workshop_area: undefined,
        add_time: ''
      },
      rules: {
        jt_org_id: [
          { required: true, message: '组织名称不能为空', trigger: 'blur' }
        ],
        base_name: [
          { required: true, message: '基地名称不能为空', trigger: 'blur' }
        ],
        nick_name: [
          { required: true, message: '项目简称不能为空', trigger: 'blur' }
        ],
        base_area: [
          { required: true, message: '基地面积不能为空', trigger: 'blur' }
        ],
        lab_area: [
          { required: true, message: '实验室面积不能为空', trigger: 'blur' }
        ],
        skill_peo_num: [
          { required: true, message: '技术人员数量不能为空', trigger: 'blur' }
        ],
        manage_peo_num: [
          { required: true, message: '管理人员数量不能为空', trigger: 'blur' }
        ],
        pro_peo_num: [
          { required: true, message: '生产人员数量不能为空', trigger: 'blur' }
        ],
        ass_line: [
          { required: true, message: '流水线条数不能为空', trigger: 'blur' }
        ],
        model_num: [
          { required: true, message: '模台数量不能为空', trigger: 'blur' }
        ],
        add_time: [
          { required: true, message: '创建日期不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //获取组织列表
    getOrgData () {
      DesignManageApi.getOrgList({
        params: {
          // is_page:0,
          list_or_tree: 'list'
        }
      }).then((res) => {
        this.orgArr = res.data.list;
      }).catch((error) => {
        console.log(error)
      })
    },
    //取消
    onCancel () {
      this.$refs['form'].resetFields();
      this.catchBack();
    },
    //查看详情
    lookBaseDetail () {
      ProductManageApi.lookBaseDetail({
        params: {
          id: this.detailObj.base_id
        }
      }).then((res) => {
        this.requestParams = res.data;
        this.requestParams.add_time = this.detailObj.add_time == null ? '' : this.detailObj.add_time.split(' ')[0]//三目判断
      }).catch((error) => {
        console.log(error)
      })
    },
    //确定
    onSure (data) {
      this.$refs[data].validate((valid) => {
        if (valid) {
          if (this.todos == 1) {
            ProductManageApi.addBase({
              data: this.requestParams
            }).then((res) => {
              if (res.code == 0) {
                this.$message.success('操作成功')
                this.catchSave()
              }
            }).catch((error) => {
              console.log(error)
            })
          } else if (this.todos == 2) {
            this.requestParams.id = this.requestParams.base_id;
            ProductManageApi.editBase({
              data: this.requestParams
            }).then((res) => {
              if (res.code == 0) {
                this.$message.success('操作成功')
                this.catchSave()
              }
            }).catch((error) => {
              console.log(error)
            })
          }
        } else {
          console.log('error')
        }
      })
    }
  },
  watch: {//监听
    todos (n, o) {
      if (n == 0) {
        this.requestParams.add_time = moment().format('YYYY-MM-DD');//获取当前系统的时间
        this.requestParams.nick_name = '';
        this.requestParams.lab_area = undefined;
        this.requestParams.yard_area = undefined;
        this.requestParams.workshop_area = undefined;
        this.requestParams.design_capa = undefined;
        this.$refs['form'].resetFields();
      } else {
        this.getOrgData();
      }
      if (n == 2 | n == 3) {
        this.lookBaseDetail()
      }
      console.log('watch-todos=', n, 'this.requestParams=', this.requestParams)
    }
  },
  created () {
    this.requestParams.add_time = moment().format('YYYY-MM-DD');//获取当前系统的时间
    this.getOrgData();
    if (this.todos == 2 | this.todos == 3) {
      this.lookBaseDetail()
    }
    console.log('created-todos=', this.todos, 'this.requestParams=', this.requestParams)
  }
}
</script>

<style lang="scss" scoped>
.bottom-box {
  text-align: right;
  margin-top: 4px;
}
.select-style {
  width: 100% !important;
}
.el-col-12 {
  padding-left: 0px !important;
}
p {
  margin-top: 18px;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.85);
  opacity: 1;
}
/deep/ .el-input-number--medium {
  width: 100% !important;
}
</style>